package com.itqf.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/13 17:23
 * description:
 */
public class ReflectionDemo {


    public static void main(String[] args) {

        //1.获取类的模板对象
        Class<AdminRoleController> clazz = AdminRoleController.class;

        //获取注解: 类 class对象获取 方法 method对象获取  属性 field对象获取
        //获取注解
        RequestMapping annotation = clazz.getAnnotation(RequestMapping.class);
        //获取注解的属性  注解.属性名()
        String base = annotation.value()[0];
        System.out.println("base = " + base);

        //2.获取所有方法
        Method[] methods = clazz.getMethods();
        Set<String> permissions = new HashSet<>();

        for (Method method : methods) {

            boolean get = method.isAnnotationPresent(GetMapping.class);
            boolean post = method.isAnnotationPresent(PostMapping.class);

            if (get||post){

            String permission = "";
            if (get){
                permission += "GET ";
                GetMapping getMapping = method.getAnnotation(GetMapping.class);
                String getUrl = getMapping.value()[0];
                permission=permission+base+getUrl;
            }else{
                permission += "POST ";
                PostMapping postMapping = method.getAnnotation(PostMapping.class);

                String postUrl = postMapping.value()[0];
                permission = permission + base + postUrl;
            }


            permissions.add(permission);
            }
        }


        System.out.println("permissions = " + permissions);

    }

}
